import configparser,os
import pymysql
class ConfigReader:
    def __init__(self,filename):
        self.config = configparser.ConfigParser() # 实例化对象
        config_path = os.path.split(os.path.realpath(__file__))[0] + os.sep + filename
        # print(config_path)
        self.config.read(config_path,'utf-8') # 读取文件名称  ，我传入的是一个config.ini的文件
    def get_options(self,option): # todo：获取某一个选项
        for section in self.config.sections(): # 遍历所有的节
            if self.config.has_option(section,option): # 如果说某一个节和我传入的option命中了，那会是True
                return self.config.get(section,option) # 返回这个选项的value值

class DB:
    def __init__(self,database_name):
        self.database_name = database_name
        self.config = ConfigReader('dev_database.ini')
        self.host = self.config.get_options(database_name + "_host") # options--->abc_host ，拿到的value--->47.113.145.193
        self.user = self.config.get_options(database_name + "_user")
        self.password = self.config.get_options(database_name + "_password")
        self.port = self.config.get_options(database_name + "_port")
    def connect(self):
        """
        print(f"数据的名称为：{self.database_name}，ip地址为：{self.host}，用户：{self.user}，密码：{self.password}，端口号：{self.port}")
        """
        self.db = pymysql.connect(user=self.user,password=self.password,host=self.host,port=int(self.port),database=self.database_name,cursorclass =
            pymysql.cursors.DictCursor)
        self.cursor = self.db.cursor()
    def close(self):
        self.cursor.close()
        self.db.close()
    def select_one(self,sql):
        result = 0
        self.connect()
        try:
            self.cursor.execute(sql)
            result = self.cursor.fetchone()
            self.close()
            return result
        except:
            self.db.rollback()
            return result
    def select_all(self,sql):
        result = 0
        self.connect()
        try:
            self.cursor.execute(sql)
            result = self.cursor.fetchall()
            self.close()
            return result
        except:
            self.db.rollback()
            return result
    def __edit(self,sql):
        result = 0
        self.connect()
        try:
            self.cursor.execute(sql)
            self.db.commit()
            self.close()
            result = 1
            return result
        except:
            self.db.rollback()
            return result
    def insert(self,sql):
        return self.__edit(sql)
    def delete(self,sql):
        return self.__edit(sql)
    def update(self,sql):
        return self.__edit(sql)
if __name__ == '__main__':
    pass
    # abc = DB("abc")
    # print(abc.delete("truncate eb_user_group"))
    # 查询全部的数据
    # print(abc.select_all("select * from eb_user_group"))
    # 查询符合条件的一条数据
    # print(abc.select_one("select * from eb_user_group where group_name like '赵政权%'"))
    # 删除数据
    # print(abc.delete("delete from eb_user_group where group_name= '赵政权a25e7ffc'"))
    # 更新数据
    # print(abc.update("update eb_user_group set group_name= '李宛潇' where group_name like '赵政权%'"))
    # 查询全部的数据
    # print(abc.select_all("select * from eb_user_group"))



















